<html>
<head><meta charset="utf-8"><title>Is this a type_alias_impl_trait bug? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html">Is this a type_alias_impl_trait bug?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209630826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209630826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bram van den Heuvel <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209630826">(Sep 10 2020 at 12:30)</a>:</h4>
<p>I'm trying to figure out whether I should file a bug for the following:</p>
<div class="codehilite"><pre><span></span><code>trait SparseColumnGenerator {
    type MySparseColumn;

    fn generate_column(&amp;self) -&gt; Self::MySparseColumn;
}

struct IGenerateSparseColumns&lt;NT&gt; {
    inner_values: Vec&lt;(usize, NT)&gt;,
}
impl&lt;&#39;a, NT&gt; SparseColumnGenerator for IGenerateSparseColumns&lt;NT&gt; {
    type MySparseColumn = IAmAColumn&lt;&#39;a, NT, impl Iterator&lt;Item = &amp;&#39;a (usize, NT)&gt;&gt;;

    fn generate_column(&amp;&#39;a self) -&gt; Self::MySparseColumn {
        Self::MySparseColumn {
            inner_iter: self.inner_values.iter().filter(|&amp;(i, _)| i % 2 == 0),
        }
    }
}

struct IAmAColumn&lt;&#39;a, NT, I: Iterator&lt;Item = &amp;&#39;a (usize, NT)&gt;&gt; {
    inner_iter: I,
}
</code></pre></div>


<p>gives</p>
<div class="codehilite"><pre><span></span><code>error[E0308]: mismatched types
  --&gt; src/lib.rs:17:25
   |
13 |     type MySparseColumn = IAmAColumn&lt;&#39;a, NT, impl Iterator&lt;Item = &amp;&#39;a (usize, NT)&gt;&gt;;
   |                                              ------------------------------------- the expected opaque type
...
17 |             inner_iter: self.inner_values.iter().filter(|&amp;(i, _)| i % 2 == 0),
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found struct `Filter`
   |
   = note: expected opaque type `impl Iterator`
                   found struct `Filter&lt;std::slice::Iter&lt;&#39;_, (usize, NT)&gt;, [closure@src/lib.rs:17:57: 17:77]&gt;`

error: could not find defining uses
  --&gt; src/lib.rs:13:46
   |
13 |     type MySparseColumn = IAmAColumn&lt;&#39;a, NT, impl Iterator&lt;Item = &amp;&#39;a (usize, NT)&gt;&gt;;
   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
</code></pre></div>


<p>on nightly. Clearly, <code>Filter</code> implements <code>Iterator</code>. Am I missing something or should I file a bug?</p>



<a name="209630898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209630898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bram van den Heuvel <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209630898">(Sep 10 2020 at 12:31)</a>:</h4>
<p>(if this is not the right stream (or even platform) to ask, please let me know)</p>



<a name="209631519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209631519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209631519">(Sep 10 2020 at 12:36)</a>:</h4>
<blockquote>
<p><code>error: could not find defining uses</code></p>
</blockquote>
<p>I think you must use the <code>impl</code> type directly in the type signature of a function for rustc to recognize that it is a "defining use", aka a use of <code>impl Trait</code> that tells rustc what the actual type behind it is.</p>



<a name="209633846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209633846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209633846">(Sep 10 2020 at 12:54)</a>:</h4>
<p>hmm... yea this is supposed to work, but I remember we may have split the feature gate into one allowing <code>type Foo = impl Trait</code> and one with the more complex case you're showing here</p>



<a name="209635800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209635800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bram van den Heuvel <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209635800">(Sep 10 2020 at 13:08)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> could you give an example of such a usage? Because</p>
<div class="codehilite"><pre><span></span><code>fn main() {
    let data = IGenerateSparseColumns { inner_values: vec![(3, 0)], };
    let x = data.generate_column();
}
</code></pre></div>


<p>doesn't do the trick.</p>



<a name="209635845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209635845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bram van den Heuvel <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209635845">(Sep 10 2020 at 13:08)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=11075ac82e5f453667e8942422fa81b3">Playground</a></p>



<a name="209635966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209635966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209635966">(Sep 10 2020 at 13:09)</a>:</h4>
<p>That would be something like</p>
<div class="codehilite"><pre><span></span><code><span class="k">type</span> <span class="nc">A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">make_a</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">A</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="mi">0</span><span class="o">..</span><span class="mi">10</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p><code>make_a</code> here is a defining use for <code>A</code> as it is mentioned as return type.</p>



<a name="209636056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209636056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bram van den Heuvel <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209636056">(Sep 10 2020 at 13:10)</a>:</h4>
<p>@oli are you suggesting I might need to use a second feature? Do you happen to know which one?</p>



<a name="209636078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209636078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209636078">(Sep 10 2020 at 13:10)</a>:</h4>
<p>I don't even know if we have a feature that allows this</p>



<a name="209636095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209636095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209636095">(Sep 10 2020 at 13:10)</a>:</h4>
<p>I just remember that this is a limitation that we've had</p>



<a name="209636108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209636108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209636108">(Sep 10 2020 at 13:10)</a>:</h4>
<p>but we want to allow it</p>



<a name="209636132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209636132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bram van den Heuvel <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209636132">(Sep 10 2020 at 13:10)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> doesn't the current example already do that? The error message mentions a specific type and <code>NT</code> is specified to be <code>i32</code> with that <code>main</code> function.</p>



<a name="209636234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209636234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209636234">(Sep 10 2020 at 13:11)</a>:</h4>
<p>The problem is <code>type A = impl Trait</code> working doesn't mean that <code>type B = (i32, impl Trait);</code> will work out of the box</p>



<a name="209636336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209636336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209636336">(Sep 10 2020 at 13:12)</a>:</h4>
<p>the original impl was <code>existential type A: Trait</code>, and all the backend still thinks it works like that even if the syntax changed</p>



<a name="209637280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209637280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209637280">(Sep 10 2020 at 13:18)</a>:</h4>
<div class="codehilite"><pre><span></span><code>thread &#39;rustc&#39; has overflowed its stack
fatal runtime error: stack overflow
</code></pre></div>


<p>recursive datastructures iksde</p>



<a name="209637630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209637630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209637630">(Sep 10 2020 at 13:21)</a>:</h4>
<p>meh, I did not want to post this here</p>



<a name="209637668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20this%20a%20type_alias_impl_trait%20bug%3F/near/209637668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20this.20a.20type_alias_impl_trait.20bug.3F.html#209637668">(Sep 10 2020 at 13:21)</a>:</h4>
<p>sry <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>